package com.jaws.demo.mq;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 模拟一个MQ生产者
 */
@Component
@Slf4j
public class MqProvider {

    @Autowired
    private AmqpTemplate rabbitTemplate;

    /**
     * 模拟消息发送入口
     */
    public void send() {

        for (int i = 0; i < 1; i++) {
            // 向jaws.topic.exchange->jaws.topic.message1 发送消息
            String sendMsg1 = "this is topic.message message1";
            this.rabbitTemplate.convertAndSend("jaws.topic.exchange", "jaws.topic.message1", sendMsg1);

            // 向jaws.topic.exchange->jaws.topic.message2 发送消息
            String sendMsg2 = "this is topic.message message2";
            this.rabbitTemplate.convertAndSend("jaws.topic.exchange", "jaws.topic.message2", sendMsg2);

            // 向jaws.topic.exchange->jaws.topic.message3 发送消息
            String sendMsg3 = "this is topic.message message3";
            MessageProperties messageProperties = new MessageProperties();
            messageProperties.setExpiration("30000");
            Message message = new Message(sendMsg3.getBytes(), messageProperties);
            this.rabbitTemplate.convertAndSend("jaws.topic.exchange", "jaws.topic.message3", message);

            // 向jaws.delayed.topic.exchange->test 发送消息 并使用 x-delayed-message
            String sendMsg4 = "this is topic.message message4";
            MessageProperties messageProperties4 = new MessageProperties();
            messageProperties4.setDelay(3000);
            Message message4 = new Message(sendMsg4.getBytes(), messageProperties4);
            this.rabbitTemplate.convertAndSend("jaws.delayed.topic.exchange", "test", message4);
        }
        log.info("MqProvider=======================发送消息成功");

    }
}
